package compro.admission.form.controller;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import compro.admission.form.entity.Application;
import compro.admission.form.entity.Education;
import compro.admission.form.entity.EnglishProficiency;
import compro.admission.form.entity.PersonalInformaion;
import compro.admission.form.entity.Prerequisites;
import compro.admission.form.entity.ProfessionalExperience;
import compro.admission.form.entity.ProgrammingSkill;
import compro.admission.form.entity.SelfStudy;
import compro.admission.form.entity.User;
import compro.admission.form.service.ApplicationService;
import compro.admission.form.service.UserService;

@Controller
public class ApplicationController {
	private ApplicationService applicationService;
	private UserService userService;

	public ApplicationService getApplicationService() {
		return applicationService;
	}

	@Resource
	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public UserService getUserService() {
		return userService;
	}

	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@RequestMapping("/showApplication")
	public String showApplication(HttpSession session) {
		User user = (User) session.getAttribute("user");
		Application application = this.applicationService
				.getApplicationByUser(user);
		session.setAttribute("application", application);
		return "showApplication";
	}

	@RequestMapping("/submitApplication")
	public String submitApplication(HttpSession session) {
		User oldUser = (User) session.getAttribute("user");
		User user = userService.getUserLogin(oldUser);
		Application application = this.applicationService
				.getApplicationByUser(user);
		applicationService.submitApplication(application);
		return "redirect:/showApplication";
	}

	@RequestMapping(value = "/apply")
	public String apply(HttpSession session, ModelMap model)
			throws IllegalArgumentException, IllegalAccessException {
		User user = (User) session.getAttribute("user");
		Application application = (Application) session
				.getAttribute("application");
		if (application == null) {
			application = applicationService.createApplication(user);
			session.setAttribute("user", user);
			session.setAttribute("application", application);
		}

		/*-------------------------------------------------------------------
		 1. Personal Information
		 -------------------------------------------------------------------*/
		PersonalInformaion personalInformaion = applicationService
				.getPersonalInformation(application);
		if (personalInformaion != null) {
			model.addAttribute("personalInformaion", personalInformaion);
		} else {
			model.addAttribute("personalInformaion", new PersonalInformaion());
		}

		/*-------------------------------------------------------------------
		 2. English Proficiency
		 -------------------------------------------------------------------*/
		EnglishProficiency englishProficiency = applicationService
				.getEnglishProficiency(application);
		if (englishProficiency != null) {
			model.addAttribute("englishProficiency", englishProficiency);
		} else {
			model.addAttribute("englishProficiency", new EnglishProficiency());
		}

		/*-------------------------------------------------------------------
		 3. Education History
		 -------------------------------------------------------------------*/
		Education education = applicationService.getEducation(application);
		if (education != null) {
			model.addAttribute("education", education);
		} else {
			model.addAttribute("education", new Education());
		}

		/*-------------------------------------------------------------------
		 4. Programming Skill
		 -------------------------------------------------------------------*/
		ProgrammingSkill programming = applicationService
				.getProgrammingSkill(application);
		if (programming != null) {
			model.addAttribute("programmingSkill", programming);
		} else {
			model.addAttribute("programmingSkill", new ProgrammingSkill());
		}

		Prerequisites prerequisites = applicationService
				.getPrerequisites(application);
		if (prerequisites != null) {
			model.addAttribute("prerequisites", prerequisites);
			List<Object> checkboxValues = new ArrayList<Object>();
			for (Field field : prerequisites.getClass().getDeclaredFields()) {
				field.setAccessible(true);
				String value = field.get(prerequisites) + "";
				if (value != null && value.length() < 30 && value.length() > 1) {
					checkboxValues.add(value);
				}
			}
			model.addAttribute("checkboxValues", checkboxValues);
		} else {
			model.addAttribute("prerequisites", new Prerequisites());
		}

		ProfessionalExperience professionalExperience = applicationService
				.getProfessionalExperience(application);
		if (professionalExperience != null) {
			model.addAttribute("professionalExperience", professionalExperience);
		} else {
			model.addAttribute("professionalExperience",
					new ProfessionalExperience());
		}

		/*-------------------------------------------------------------------
		 7. Self Study
		 -------------------------------------------------------------------*/
		SelfStudy selfStudy = applicationService.getSelfStudy(application);
		if (selfStudy != null) {
			model.addAttribute("selfStudy", selfStudy);
		} else {
			model.addAttribute("selfStudy", new SelfStudy());
		}

		return "application";
	}

	@RequestMapping(value = "/submitPrerequisite", method = RequestMethod.POST)
	public String submitPrerequisite(Prerequisites prerequisites,
			HttpSession session) {
		Application application = (Application) session
				.getAttribute("application");
		applicationService.savePrerequisite(prerequisites, application);
		return "true";
	}

	@RequestMapping(value = "/submitPersonal", method = RequestMethod.POST)
	public String submitPersonalInformation(
			PersonalInformaion personalInformaion, HttpSession session) {
		Application application = (Application) session
				.getAttribute("application");
		applicationService.savePersonalInformaion(personalInformaion,
				application);
		return "true";
	}

	@RequestMapping(value = "/submitSelfStudy", method = RequestMethod.POST)
	public String submitSelfStudy(SelfStudy selfStudy, HttpSession session) {
		Application application = (Application) session
				.getAttribute("application");
		applicationService.saveSelfStudy(selfStudy, application);
		return "true";
	}

	@RequestMapping(value = "/submitProfessional", method = RequestMethod.POST)
	public String submitProfessional(
			ProfessionalExperience professionalExperience, HttpSession session) {
		Application application = (Application) session
				.getAttribute("application");
		applicationService.saveProfessionalExperience(professionalExperience,
				application);
		return "true";
	}

	@RequestMapping(value = "/submitEducation", method = RequestMethod.POST)
	public String submitEducation(Education education, HttpSession session) {
		Application application = (Application) session
				.getAttribute("application");
		applicationService.saveEducation(education, application);
		return "true";
	}

	@RequestMapping(value = "/submitEnglish", method = RequestMethod.POST)
	public String submitEnglishProficiency(
			EnglishProficiency englishProficiency, HttpSession session) {
		Application application = (Application) session
				.getAttribute("application");
		applicationService.saveEnglishProficiency(englishProficiency,
				application);
		return "true";
	}

	@RequestMapping(value = "/submitProgramming", method = RequestMethod.POST)
	public String submitProgrammingSkill(ProgrammingSkill programmingSkill,
			HttpSession session) {
		Application application = (Application) session
				.getAttribute("application");
		applicationService.saveProgrammingSkill(programmingSkill, application);
		return "true";
	}

	@RequestMapping(value = "/requestReset")
	public String requestResertApplication(HttpSession session, ModelMap model) {
		return "request_reset";
	}

	@RequestMapping(value = "/sendRequest", method = RequestMethod.POST)
	public String sendRequest(HttpSession session, @RequestParam String reason,
			ModelMap model) {
		Application application = (Application) session
				.getAttribute("application");
		application.setNote(reason);
		applicationService.requestResetApplication(application);
		model.addAttribute("title", "Request to Reset Application.");
		model.addAttribute("msg",
				"You request is sent to admission staff successfully.");
		return "success";
	}

}
